package com.jing.weibo.ws.sina;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import weibo4j.Comments;
import weibo4j.Friendships;
import weibo4j.Oauth;
import weibo4j.Search;
import weibo4j.Timeline;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import weibo4j.model.Status;
import weibo4j.model.User;
import weibo4j.model.UserWapper;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONObject;

import com.jing.system.comm.contants.WeiboCons;
import com.jing.weibo.ws.sina.utils.OauthUtil;
import com.jing.weibo.ws.sina.utils.UserUtil;

/**
 * 新浪微博的接口
 * @author jing.yue
 * @version 2012/08/29 1.0.0
 */
@Component
public class SinaService {
	private static final Logger logger = Logger.getLogger(SinaService.class);

	private Oauth oauth;
	private Timeline timeline;
	private Weibo weibo;
	private Users user;
	private Friendships friendships;
	private Comments comments;
	private Search search;

	/**
	 * 构造函数实例对象
	 */
	public SinaService() {
		this.oauth = new Oauth();
		this.timeline = new Timeline();
		this.weibo = new Weibo();
		this.user = new Users();
		this.friendships = new Friendships();
		this.comments = new Comments();
		this.search = new Search();
	}

	/**
	 * 根据code获取用户授权的信息
	 * @param code
	 * @return
	 * @throws WeiboException
	 */
	public AccessToken getAccessTokenByCode(String code) throws WeiboException {
		// TODO Auto-generated method stub
		return OauthUtil.getAccessTokenByCode(code, oauth);
	}

	/**
	 * 根据用户ID获取用户信息
	 *
	 * @param uid			需要查询的用户ID
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public User showUserById(String uid, String token) throws WeiboException {
		// TODO Auto-generated method stub
		return UserUtil.showUserById(uid, token, user);
	}

	/**
	 * 根据uid获取用户的粉丝
	 * @param uid
	 * @param count
	 * @param cursor
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public UserWapper getFollowersById(String uid, Integer count, Integer cursor, String token) throws WeiboException {
		// TODO Auto-generated method stub
		return friendships.getFollowersById(uid, count, cursor, token);
	}

	/**
	 * 根据uid获取用户所有的粉丝
	 * @param uid
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public List<UserWapper> getFollowersByIdAll(String uid, String token) throws WeiboException {
		// TODO Auto-generated method stub
		List<UserWapper> list = new ArrayList<UserWapper>();
		UserWapper userWapper = friendships.getFollowersById(uid, WeiboCons.WEIBO_USER_FANS_ATTENTION_PAGE_SIZE, 0, token);
		list.add(userWapper);
		if(userWapper.getNextCursor() > 0l) {
			while(true) {
				userWapper = friendships.getFollowersById(uid, WeiboCons.WEIBO_USER_FANS_ATTENTION_PAGE_SIZE, Long.valueOf(userWapper.getNextCursor()).intValue(), token);
				//不存在数据
				if(userWapper == null || userWapper.getUsers() == null || userWapper.getUsers().size() == 0) {
					break;
				}
				//不存在下一页
				else if(userWapper.getNextCursor() <= 0l) {
					list.add(userWapper);
					break;
				}
				else {
					list.add(userWapper);
				}
			}
		}
		return list;
	}

	/**
	 * 根据uid获取用户关注列表
	 * @param uid
	 * @param count
	 * @param cursor
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public UserWapper getFriendsByID(String uid, Integer count, Integer cursor, String token) throws WeiboException {
		// TODO Auto-generated method stub
		return friendships.getFriendsByID(uid, count, cursor, token);
	}

	/**
	 * 根据uid获取用户所有的关注列表
	 * @param uid
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public List<UserWapper> getFriendsByIDAll(String uid, String token) throws WeiboException {
		// TODO Auto-generated method stub
		List<UserWapper> list = new ArrayList<UserWapper>();
		UserWapper userWapper = friendships.getFriendsByID(uid, WeiboCons.WEIBO_USER_FANS_ATTENTION_PAGE_SIZE, 0, token);
		list.add(userWapper);
		if(userWapper.getNextCursor() > 0l) {
			while(true) {
				userWapper = friendships.getFriendsByID(uid, WeiboCons.WEIBO_USER_FANS_ATTENTION_PAGE_SIZE, Long.valueOf(userWapper.getNextCursor()).intValue(), token);
				//不存在数据
				if(userWapper == null || userWapper.getUsers() == null || userWapper.getUsers().size() == 0) {
					break;
				}
				//不存在下一页
				else if(userWapper.getNextCursor() <= 0l) {
					list.add(userWapper);
					break;
				}
				else {
					list.add(userWapper);
				}
			}
		}
		return list;
	}

	/**
	 * 取消关注一个用户
	 * @param uid
	 * @param token			授权的token
	 * @throws WeiboException
	 */
	public User destroyFriendshipsDestroyById(String uid, String token) throws WeiboException {
		// TODO Auto-generated method stub
		return friendships.destroyFriendshipsDestroyById(uid, token);
	}

	/**
	 * 关注一个用户
	 * @param uid
	 * @param token
	 * @throws WeiboException
	 */
	public User createFriendshipsById(String uid, String token) throws WeiboException {
		// TODO Auto-generated method stub
		return friendships.createFriendshipsById(uid, token);
	}

	/**
	 * 获取两个用户之间的详细关注关系情况
	 * @param sourceId	源用户的UID
	 * @param targetId	目标用户的UID
	 * @param token		授权的token
	 * @return
	 * @throws WeiboException
	 */
	public JSONObject showFriendship(String sourceId, String targetId, String token) throws WeiboException {
		// TODO Auto-generated method stub
		return friendships.showFriendship(sourceId, targetId, token);
	}

	/**
	 * 发布一条新微博
	 * @param status 		要发布的微博文本内容，必须做URLencode，内容不超过140个汉字
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public Status updateStatus(String status, String token) throws WeiboException {
		// TODO Auto-generated method stub
		return timeline.UpdateStatus(status, token);
	}
}
